package com.magic.item.service.impl;

import com.magic.item.mapper.SpecGroupMapper;
import com.magic.item.mapper.SpecParamMapper;
import com.magic.item.service.SpecificationService;
import com.magic.item.pojo.SpecGroup;
import com.magic.item.pojo.SpecParam;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lan.jianyuan
 * @create 2021-01-12-20:13
 */
@Service
public class SpecificationServiceImpl implements SpecificationService {
    @Resource
    private SpecGroupMapper specGroupMapper;
    @Resource
    private SpecParamMapper specParamMapper;
    /**
     *根据分类id查询参数组
     * @param cid
     * @return
     */
    
    public List<SpecGroup> queryGroupsByCid(Long cid) {
        SpecGroup record = new SpecGroup();
        record.setCid(cid);
        return this.specGroupMapper.select(record);
    }
    /**
     * 根据条件查询规格参数
     * @param gid
     * @param cid
     * @param generic
     * @param searching
     * @return
     */
    public List<SpecParam> queryParams(Long gid, Long cid, Boolean generic, Boolean searching) {
        SpecParam record = new SpecParam();
        record.setGroupId(gid);
        record.setCid(cid);
        record.setSearching(searching);
        record.setGeneric(generic);
        return this.specParamMapper.select(record);
        
    }
    
    /**
     * 新增规格参数
     * @param specParam
     * @return
     */
    @Override
    @Transactional
    public void addSpecParam(SpecParam specParam) {
        specParam.setId(null);
        this.specParamMapper.insertSelective(specParam);
    }
    /**
     * 修改规格参数
     * @param specParam
     * @return
     */
    @Override
    @Transactional
    public void updateSpecParam(SpecParam specParam) {
        this.specParamMapper.updateByPrimaryKey(specParam);
    }
    /**
     * 删除规格参数
     * @param pid
     * @return
     */
    @Override
    @Transactional
    public void deleteSpecParam(Long pid) {
        this.specParamMapper.deleteSpecParam(pid);
    }
    /**
     * 新增分组
     * @param specGroup
     * @return
     */
    @Override
    @Transactional
    public void saveSpecGroup(SpecGroup specGroup) {
        specGroup.setId(null);
        this.specGroupMapper.insertSelective(specGroup);
    }
    /**
     * 修改分组
     * @param specGroup
     * @return
     */
    @Override
    @Transactional
    public void updateSpecGroup(SpecGroup specGroup) {
        this.specGroupMapper.updateByPrimaryKey(specGroup);
    }
    /**
     * 删除参数为空的分组
     * @param gid
     * @return
     */
    @Override
    @Transactional
    public void deleteSpecGroup(Long gid) {
        List<SpecParam> specParams = specParamMapper.selectSpecParam(gid);
        if (CollectionUtils.isEmpty(specParams)) {
            this.specGroupMapper.deleteSpecGroup(gid);
        }
    }
    
    public List<SpecGroup> querySpecsByCid(Long cid) {
        // 查询规格组
        List<SpecGroup> groups = this.queryGroupsByCid(cid);
        SpecParam param = new SpecParam();
        groups.forEach(group -> {
            // 查询组内参数
            group.setParams(this.queryParams(group.getId(), null, null, null));
        });
        return groups;
    }
}
